import { defineStore } from 'pinia'
import { diyEnum, popupEnum } from '~/enums/app'
import appApi from '~/api/public'

interface AppSate {
    popupType: popupEnum;
    isMobile: boolean;
    monitor: AppMonitorResponse;
    models: AppAiModelsResponse;
    config: AppConfigResponse;
    diyDto: Record<number, AppDiyResponse>;
}

const useAppStore = defineStore('appStore', {
    state: (): AppSate => ({
        popupType: popupEnum.NULL,
        isMobile: false,
        monitor: {} as AppMonitorResponse,
        models: {} as AppAiModelsResponse,
        config: {} as AppConfigResponse,
        diyDto: {}
    }),
    getters: {
        getAmountUnit: (state: AppSate) => state.config.dialog?.currency_unit || '积分',
        getBulletinConfig: (state: AppSate) => state.config.bulletin || {},
        getWebsiteConfig: (state: AppSate) => state.config.website || {},
        getSwitchConfig: (state: AppSate) => state.config.switch || {},
        getDialogConfig: (state: AppSate) => state.config.dialog || {},
        getLoginConfig: (state: AppSate) => state.config.login || {},
        getPcConfig: (state: AppSate) => state.config.pc || {}
    },
    actions: {
        /**
         * 补全文件地址
         */
        toAbsoluteUrl(url: string): string {
            return url ? `${this.config?.oss_domain}/${url}` : ''
        },
        /**
         * 获取全局配置
         */
        async getConfig(): Promise<AppConfigResponse> {
            this.config = await appApi.config()
            return this.config
        },
        /**
         * 获取模型列表
         */
        async getModels(): Promise<AppAiModelsResponse> {
            this.models = await appApi.models()
            return this.models
        },
        /**
         * 获取装修数据
         *
         * @param scene
         * @returns {Promise<AppDiyResponse|any>}
         */
        async getDiy(scene: number): Promise<AppDiyResponse | any> {
            if (this.diyDto[scene]) {
                return this.diyDto[scene]
            }
            const detail: AppDiyResponse = await appApi.diy(scene)
            this.diyDto[scene] = detail
            return detail
        },
        /**
         * 获取消息数据
         */
        async getMonitor(): Promise<AppMonitorResponse> {
            this.monitor = await appApi.monitor()
            return this.monitor
        },
        /**
         * 初始系统数据
         */
        async initialize(): Promise<any> {
            await this.getDiy(diyEnum.DESK)
        },
        /**
         * 设置显示弹窗
         */
        setPopup(type: popupEnum): void {
            this.popupType = type
        },
        /**
         * 设置手机模式
         */
        setMobile(value: boolean): void {
            this.isMobile = value
        }
    }
})

export default useAppStore
